from Links import Links,Link,Links_test
from Routes import Route,Routes,Routes_test
from Trajectories import Seq_pair,Trajectories,Trajectorie,Trajectories_test
from Volumes import Volumes,Volume,Volumes_test
from Weathers import Weathers,Weather,Weathers_test
import re,datetime

path='./data/'

def readLinks(links):
    filename='links.csv'
    fr=open(path+filename,'r')
    fr.readline()
    data=fr.readlines()
    fr.close()
    for d in data:
        l=re.sub('^"|"\n$','',d).split('","')
        if not l[4].isdigit():
            ll=l[4].split(',')
            l[4]=[]
            if ll!=['']:
                for i in ll:
                    l[4].append(int(i))
        else:
            l[4]=[int(l[4])]
        if not l[5].isdigit():
            ll=l[5].split(',')
            l[5]=[]
            if ll!=['']:
                for i in ll:
                    l[5].append(int(i))
        else:
            l[5]=[int(l[5])]
        k=Link(int(l[0]),int(l[1]),int(l[2]),int(l[3]),l[4],l[5],int(l[6]))
        links.add_link(k)
    print 'read links done.'

def readRoutes(routes):
    filename='routes.csv'
    fr=open(path+filename,'r')
    fr.readline()
    data=fr.readlines()
    fr.close()
    for d in data:
        l=re.sub('^"|"\n$','',d).split('","')
        ll=l[2].split(',')
        l[2]=[]
        for i in ll:
            l[2].append(int(i))
        k=Route(l[0],int(l[1]),l[2])
        routes.add_route(k)
    print 'read routes done.'

def readTrajectories(trajectories):
    filename='trajectories.csv'
    fr=open(path+filename,'r')
    fr.readline()
    data=fr.readlines()
    fr.close()
    tot=0
    for d in data:
        l=re.sub('^"|"\n$','',d).split('","')
        start_time=datetime.datetime.strptime(l[3],"%Y-%m-%d %H:%M:%S")
        k=Trajectorie(l[0],int(l[1]),int(l[2]),start_time,float(l[5]))
        ll=l[4].split(';')
        for i in ll:
            o=i.split('#')
            k.add_travel_seq(Seq_pair(int(o[0]),datetime.datetime.strptime(o[1],"%Y-%m-%d %H:%M:%S"),float(o[2])))
        trajectories.add_trajectorie(k)
        tot+=1
        if tot%10000==0:
            print 'trajectories:',tot
    print 'read trajectories done.'

def readVolumes(volumes):
    filename='volume.csv'
    fr=open(path+filename,'r')
    fr.readline()
    data=fr.readlines()
    fr.close()
    tot=0
    for d in data:
        l=re.sub('^"|"\n$','',d).split('","')
        rec_time=datetime.datetime.strptime(l[0],"%Y-%m-%d %H:%M:%S")
        if l[5]=='':
            l[5]='-1'
        k=Volume(rec_time,int(l[1]),int(l[2]),int(l[3]),int(l[4]),int(l[5]))
        volumes.add_volume(k)
        tot+=1
        if tot%10000==0:
            print 'volumes:',tot
    print 'read volumes done.'

def readWeathers(weathers):
    filename='weather.csv'
    fr=open(path+filename,'r')
    fr.readline()
    data=fr.readlines()
    fr.close()
    tot=0
    for d in data:
        l=re.sub('^"|"\n$','',d).split('","')
        ll=l[0].split('-')
        rec_time=datetime.date(int(ll[0]),int(ll[1]),int(ll[2]))
        k=Weather(rec_time,int(l[1]),float(l[2]),float(l[3]),float(l[4]),float(l[5]),float(l[6]),float(l[7]),float(l[8]))
        weathers.add_weather(k)
    print 'read weathers done.'

def readTrajectories2(trajectories):
    filename='trajectories_test.csv'
    fr=open(path+filename,'r')
    fr.readline()
    data=fr.readlines()
    fr.close()
    tot=0
    for d in data:
        l=re.sub('^"|"\n$','',d).split('","')
        start_time=datetime.datetime.strptime(l[3],"%Y-%m-%d %H:%M:%S")
        k=Trajectorie(l[0],int(l[1]),int(l[2]),start_time,float(l[5]))
        ll=l[4].split(';')
        for i in ll:
            o=i.split('#')
            k.add_travel_seq(Seq_pair(int(o[0]),datetime.datetime.strptime(o[1],"%Y-%m-%d %H:%M:%S"),float(o[2])))
        trajectories.add_trajectorie(k)
        tot+=1
        if tot%10000==0:
            print 'trajectories:',tot
    print 'read trajectories done.'

def readVolumes2(volumes):
    filename='volume_test.csv'
    fr=open(path+filename,'r')
    fr.readline()
    data=fr.readlines()
    fr.close()
    tot=0
    for d in data:
        l=re.sub('^"|"\n$','',d).split('","')
        rec_time=datetime.datetime.strptime(l[0],"%Y-%m-%d %H:%M:%S")
        if l[5]=='':
            l[5]='-1'
        k=Volume(rec_time,int(l[1]),int(l[2]),int(l[3]),int(l[4]),int(l[5]))
        volumes.add_volume(k)
        tot+=1
        if tot%10000==0:
            print 'volumes:',tot
    print 'read volumes done.'

def readWeathers2(weathers):
    filename='weather_test.csv'
    fr=open(path+filename,'r')
    fr.readline()
    data=fr.readlines()
    fr.close()
    tot=0
    for d in data:
        l=re.sub('^"|"\n$','',d).split('","')
        ll=l[0].split('-')
        rec_time=datetime.date(int(ll[0]),int(ll[1]),int(ll[2]))
        k=Weather(rec_time,int(l[1]),float(l[2]),float(l[3]),float(l[4]),float(l[5]),float(l[6]),float(l[7]),float(l[8]))
        weathers.add_weather(k)
    print 'read weathers done.'
